其他
R可视化13|ggplot2-图层图形语法 (5)
"pythonic生物人"的第111篇分享
本文系统介绍ggplot2的自带主题(Themes)及主题个性化,续前篇。
本文目录
11、主题(Themes)
自带主题
个性化主题
背景|标题|图边框 (Plot elements)
坐标轴设置 (Axis elements)
图例(Legend elements)
画板设置( Panel elements)
分面(Facetting elements)
11、主题(Themes)
Python中Matplotlib和Seaborn关于主题的设置见:
Python可视化19|seborn图形外观设置
Python可视化|matplotlib04-绘图风格(plt.style)大全
自带主题
ggthemes中包含34种主题,各个主题长什么样子了?
options(repr.plot.width = 5.5, repr.plot.height = 28, repr.plot.res = 300)
library(dplyr)
library(ggthemes)
base <- ggplot(mpg, aes(displ, hwy)) +
geom_point() +
geom_smooth()
p1 <- base+theme_base()+ggtitle("theme_base()")
p2 <- base+theme_bw()+ggtitle("theme_bw()")
p3 <- base+theme_calc()+ggtitle("theme_calc()")
p4 <- base+theme_classic()+ggtitle("theme_classic()")
p5 <- base+theme_clean()+ggtitle("theme_clean()")
p6 <- base+theme_dark()+ggtitle("theme_dark()")
p7 <- base+theme_economist()+ggtitle("theme_economist()")
p8 <- base+theme_economist_white()+ggtitle("theme_economist_white()")
p9 <- base+theme_excel()+ggtitle("theme_excel()")
p10 <- base+theme_excel_new()+ggtitle("theme_excel_new()")
p11 <- base+theme_few()+ggtitle("theme_few()")
p12 <- base+theme_fivethirtyeight()+ggtitle("theme_fivethirtyeight()")
p13 <- base+theme_foundation()+ggtitle("theme_foundation()")
p14 <- base+theme_gdocs()+ggtitle("theme_gdocs()")
p15 <- base+theme_get()+ggtitle("theme_get()")
p16 <- base+theme_gray()+ggtitle("theme_gray()")
p17 <- base+theme_grey()+ggtitle("theme_grey()")
p18 <- base+theme_hc()+ggtitle("theme_hc()")
p19 <- base+theme_igray()+ggtitle("theme_igray()")
p20 <- base+theme_light()+ggtitle("theme_light()")
p21 <- base+theme_linedraw()+ggtitle("theme_linedraw()")
p22 <- base+theme_map()+ggtitle("theme_map()")
p23 <- base+theme_minimal()+ggtitle("theme_minimal()")
p24 <- base+theme_pander()+ggtitle("theme_pander()")
p25 <- base+theme_par()+ggtitle("theme_par()")
p26 <- base+theme_replace()+ggtitle("theme_replace()")
p28 <- base+theme_solarized()+ggtitle("theme_solarized()")
p29 <- base+theme_solarized_2()+ggtitle("theme_solarized_2()")
p30 <- base+theme_solid()+ggtitle("theme_solid()")
p31 <- base+theme_stata()+ggtitle("theme_stata()")
p32 <- base+theme_test()+ggtitle("theme_test()")
p33 <- base+theme_tufte()+ggtitle("theme_tufte()")
p34 <- base+theme_update()+ggtitle("theme_update()")
p35 <- base+theme_void()+ggtitle("theme_void()")
p36 <- grid.arrange(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16, p17, p18, p19, p20, p21, p22, p23, p24, p25, p26, p28, p29, p30, p31, p32, p33, p34, p35,nrow = 17)
ggsave("theme.png", p36, width = 5.5, height = 28)
个性化主题
可通过修改theme函数中的如下参数,进而个性化绘图主题。
> theme
#以下()中的参数都可自由修改
function (line, rect, text, title, aspect.ratio, axis.title,
axis.title.x, axis.title.x.top, axis.title.x.bottom, axis.title.y,
axis.title.y.left, axis.title.y.right, axis.text, axis.text.x,
axis.text.x.top, axis.text.x.bottom, axis.text.y, axis.text.y.left,
axis.text.y.right, axis.ticks, axis.ticks.x, axis.ticks.x.top,
axis.ticks.x.bottom, axis.ticks.y, axis.ticks.y.left, axis.ticks.y.right,
axis.ticks.length, axis.ticks.length.x, axis.ticks.length.x.top,
axis.ticks.length.x.bottom, axis.ticks.length.y, axis.ticks.length.y.left,
axis.ticks.length.y.right, axis.line, axis.line.x, axis.line.x.top,
axis.line.x.bottom, axis.line.y, axis.line.y.left, axis.line.y.right,
legend.background, legend.margin, legend.spacing, legend.spacing.x,
legend.spacing.y, legend.key, legend.key.size, legend.key.height,
legend.key.width, legend.text, legend.text.align, legend.title,
legend.title.align, legend.position, legend.direction, legend.justification,
legend.box, legend.box.just, legend.box.margin, legend.box.background,
legend.box.spacing, panel.background, panel.border, panel.spacing,
panel.spacing.x, panel.spacing.y, panel.grid, panel.grid.major,
panel.grid.minor, panel.grid.major.x, panel.grid.major.y,
panel.grid.minor.x, panel.grid.minor.y, panel.ontop, plot.background,
plot.title, plot.title.position, plot.subtitle, plot.caption,
plot.caption.position, plot.tag, plot.tag.position, plot.margin,
strip.background, strip.background.x, strip.background.y,
strip.placement, strip.text, strip.text.x, strip.text.y,
strip.switch.pad.grid, strip.switch.pad.wrap, ..., complete = FALSE,
validate = TRUE)
{。。。。。。。。。。。。。。。。。。。。。。。。。
}
以下分类介绍。
背景|标题|图边框 (Plot elements)
Element | Setter(每个函数中有若干参数可修改) | Description |
---|---|---|
plot.background | element_rect() | plot background |
plot.title | element_text() | plot title |
plot.margin | margin() | margins around plot |
拿 element_text()、element_rect()、margin()为例,看看有哪些参数可设置,完全不需要记住这些参数,只需在R件中输入对应Setter即可查看所有可修改参数。
> element_text()
List of 11
$ family : NULL
$ face : NULL
$ colour : NULL
$ size : NULL
$ hjust : NULL
$ vjust : NULL
$ angle : NULL
$ lineheight : NULL
$ margin : NULL
$ debug : NULL
$ inherit.blank: logi FALSE
- attr(*, "class")= chr [1:2] "element_text" "element"
> element_rect()
List of 5
$ fill : NULL
$ colour : NULL
$ size : NULL
$ linetype : NULL
$ inherit.blank: logi FALSE
- attr(*, "class")= chr [1:2] "element_rect" "element"
> margin()
[1] 0points 0points 0points 0points
举个栗子
options(repr.plot.width = 4.5, repr.plot.height = 3, repr.plot.res = 300)
base <- ggplot(mpg, aes(displ, hwy)) +
geom_point() +
geom_smooth() + labs(title = "Comeon man")
p1 <- base
p2 <- base + theme(plot.background = element_rect(fill = "#74C476", colour = "red"))#背景色设置
p3 <- base + theme(plot.title = element_text(face = "bold", colour = "blue"))#标题设置
p4 <- base + theme(plot.margin = margin(1, 2, 0.5, 2))#设置绘图区域大小
p5 <- grid.arrange(p1, p2, p3, p4,nrow = 2)
ggsave("theme1.png", p5, width = 4.5, height = 3)
坐标轴设置 (Axis elements)
Element | Setter | Description |
---|---|---|
axis.line | element_line() | line parallel to axis (hidden in default themes) |
axis.text | element_text() | tick labels |
axis.text.x | element_text() | x-axis tick labels |
axis.text.y | element_text() | y-axis tick labels |
axis.title | element_text() | axis titles |
axis.title.x | element_text() | x-axis title |
axis.title.y | element_text() | y-axis title |
axis.ticks | element_line() | axis tick marks |
axis.ticks.length | unit() | length of tick marks |
options(repr.plot.width = 2, repr.plot.height = 6, repr.plot.res = 300)
p1 <- base + theme(axis.line = element_line(colour = "grey50", size = 1))
p2 <- base + theme(axis.text = element_text(color = "red", size = 12))
p3 <- base + theme(axis.text.x = element_text(angle = 45, vjust = 0.5))
p4 <- grid.arrange(p1, p2, p3,nrow = 1)
ggsave("theme2.png", p4, width = 2, height = 6)
图例(Legend elements)
Element | Setter | Description |
---|---|---|
legend.background | element_rect() | legend background |
legend.key | element_rect() | background of legend keys |
legend.key.size | unit() | legend key size |
legend.key.height | unit() | legend key height |
legend.key.width | unit() | legend key width |
legend.margin | unit() | legend margin |
legend.text | element_text() | legend labels |
legend.text.align | 0–1 | legend label alignment (0 = right, 1 = left) |
legend.title | element_text() | legend name |
legend.title.align | 0–1 | legend name alignment (0 = right, 1 = left) |
options(repr.plot.width = 5, repr.plot.height = 4, repr.plot.res = 300)
df <- data.frame(x = 1:4, y = 1:4, z = rep(c("a", "b"), each = 2))
p1 <- base <- ggplot(df, aes(x, y, colour = z)) + geom_point()
p2 <- base + theme(
legend.background = element_rect(
fill = "lemonchiffon",
colour = "grey50",
size = 1
)
)
p3 <- base + theme(
legend.key = element_rect(color = "grey50"),
legend.key.width = unit(0.9, "cm"),
legend.key.height = unit(0.75, "cm")
)
p4 <- base + theme(
legend.text = element_text(size = 15),
legend.title = element_text(size = 15, face = "bold")
)
p5 <- grid.arrange(p1, p2, p3, p4, nrow = 2)
ggsave("theme3.png", p5, width = 2, height = 6)
画板设置( Panel elements)
Element | Setter | Description |
---|---|---|
panel.background | element_rect() | panel background (under data) |
panel.border | element_rect() | panel border (over data) |
panel.grid.major | element_line() | major grid lines |
panel.grid.major.x | element_line() | vertical major grid lines |
panel.grid.major.y | element_line() | horizontal major grid lines |
panel.grid.minor | element_line() | minor grid lines |
panel.grid.minor.x | element_line() | vertical minor grid lines |
panel.grid.minor.y | element_line() | horizontal minor grid lines |
aspect.ratio | numeric | plot aspect ratio |
options(repr.plot.width = 7, repr.plot.height = 2, repr.plot.res = 300)
p1 <- base + theme(panel.background = element_rect(fill = "lightblue"))
p2 <-base + theme(
panel.grid.major = element_line(color = "gray60", size = 0.8)
)
p3 <-base + theme(
panel.grid.major.x = element_line(color = "gray60", size = 0.8)
)
p4 <- grid.arrange(p1, p2, p3,nrow = 1)
ggsave("theme4.png", p4, width = 5, height = 2)
分面(Facetting elements)
Element | Setter | Description |
---|---|---|
strip.background | element_rect() | background of panel strips |
strip.text | element_text() | strip text |
strip.text.x | element_text() | horizontal strip text |
strip.text.y | element_text() | vertical strip text |
panel.margin | unit() | margin between facets |
panel.margin.x | unit() | margin between facets (vertical) |
panel.margin.y | unit() | margin between facets (horizontal) |
df <- data.frame(x = 1:4, y = 1:4, z = c("a", "a", "b", "b"))
base_f <- ggplot(df, aes(x, y)) + geom_point() + facet_wrap(~z)
base_f
base_f + theme(panel.margin = unit(0.5, "in"))
#> Warning: `panel.margin` is deprecated. Please use `panel.spacing` property
#> instead
base_f + theme(
strip.background = element_rect(fill = "grey20", color = "grey80", size = 1),
strip.text = element_text(colour = "white")
)
有意见请移步到QQ群629562529反馈,一起进步哈!